<!-- 课程卡片 -->
<template>
  <div class="courseCard" @click="toDetails">
    <div class="cardBanner">
      <img
        class="cardImg"
        :src="$getImageUrl(cardData.originImg)"
        alt=""
        srcset=""
      />
      <div class="cardTag">{{ cardData.categoryName }}</div>
    </div>
    <div class="cardInfo">
      <el-popover
        v-if="type === 'personal'"
        placement="bottom-start"
        width="96px"
        trigger="hover"
      >
        <div class="removeBtn" @click="deleteMyJoinedCourseFn">移除学习</div>
        <img class="moreImg" src="@/assets/more.png" slot="reference" />
      </el-popover>
      <div>
        <div class="cardTitle moreHidden">
          {{ cardData.name }}
        </div>
        <div v-if="type === 'personal'" class="lecturer">讲师：{{ cardData.speaker || '无' }}</div>
      </div>
      <div class="cardOther">
        <div v-if="type === 'default'" class="lecturer">讲师：{{ cardData.speaker || '无' }}</div>
        <div v-if="type === 'personal'" class="lecturer">{{ cardData.status === 'END' ? '已学完' : `学到：${formatDate(cardData.learnTimes)}` }}</div>
        <div class="booking">
          <img src="@/assets/users.png" alt="" />
          {{ cardData.joins || 0 }}人{{ type === 'personal' ? '学习' : ''}}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { deleteMyJoinedCourse } from '@/api/personal';
export default {
  name: 'CourseCard',
  props: {
    cardData: {
      type: Object,
      default: () => {
        return {
          objectId: 0,
          originImg: '',
          name: '课程名称',
          speaker: '讲师名称',
          likes: 0,
          categoryName: '班级管理'
        };
      },
    },
    type: {
      type: String,
      default: 'default' // default 默认 personal 个人中心样式
    }
  },
  data() {
    return {};
  },
  methods: {
    toDetails() {
      const url = this.$router.resolve({
        path: '/courseDetails',
        query: {
          id: this.cardData.objectId
        }
      }).href;
      window.open(url, '_blank');
    },
    // 格式化
    formatDate(seconds) {
      if(!seconds) {
        return '00:00:00';
      }
      // dayjs.duration()返回一个Duration对象，可以被格式化
      return this.$dayjs.duration(seconds, 'seconds').format('HH:mm:ss', { trim: false });
    },
    async deleteMyJoinedCourseFn() {
      try {
        await deleteMyJoinedCourse({ courseId : this.cardData.objectId });
        this.$emit('updateList');
      } catch (error) {
        console.error(error);
      }

    }
    
  },
};
</script>

<style scoped lang="scss">
.courseCard {
  width: 100%;
  height: 100%;
  cursor: pointer;
  display: flex;

  .cardBanner {
    width: 120px;
    height: 100%;
    position: relative;
    background: #fff;
    overflow: hidden;
    display: flex;
    align-items: center;
    border-radius: 4px;
    .cardImg {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    .cardTag {
      color: #fff;
      padding: 4px 6px;
      border-radius: 2px;
      background: rgba(0, 0, 0, 0.5);
      position: absolute;
      top: 4px;
      left: 4px;
      font-size: 12px;
      font-weight: 600;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .cardInfo {
    flex: 1;
    padding-left: 8px;
    box-sizing: border-box;
    display: flex;
    align-content: space-between;
    flex-wrap: wrap;
    position: relative;
    .moreImg {
      position: absolute;
      top: 0;
      right: 0;
      width: 20px;
      height: 20px;
    }
    .cardTitle {
      width: 100%;
      height: 36px;
      font-size: 12px;
      font-weight: 600;
      line-height: 18px;
      color: $text-title-color;
      -webkit-line-clamp: 2;
    }
    .lecturer {
      height: 18px;
      line-height: 18px;
      font-size: 12px;
      font-weight: 400;
      color: $text-color;
    }
    .cardOther {
      width: 100%;
      display: flex;
      align-items: center;
      margin-top: 12px;
      justify-content: space-between;
      .booking {
        color: $text-color-secondary;
        font-size: 12px;
        font-weight: 400;
        line-height: 18px;
        display: flex;
        align-items: center;
        img {
          widows: 14px;
          height: 14px;
          margin-right: 4px;
        }
      }
    }
  }
}

.removeBtn {
  width: 96px;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  &:hover {
    background: #D7E8FC;
  }
}
</style>
